<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/03/22
//      |_|                                         | TpMeCMS

declare(strict_types=1);
namespace ImiApp\ApiServer\Api\Controller\com;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
// use ImiApp\ApiServer\Common\AuthApi;
use Phpben\Imi\Auth\Annotation\Auth;
use ImiApp\ApiServer\Api\Controller\Tpmecmscom;

/**
 * 意见反馈🐉
 * @Auth(name="token",nologin={"get_feedbackcate"})
 * @Controller("/com.feedback/")
 * 
 */
class Feedback extends Tpmecmscom
{
    protected $noNeedLogin = ['get_feedbackcate'];
    protected $noNeedRight = ['*'];



    /**
     * 获取意见反分类列表🐉
     * @Action
     * @Route(url="get_feedbackcate",method="GET")
     * @return void
     * 
    */
    public function get_feedbackcate()
    {
        $list = Db::name('feedbackcate')->fieldRaw('id,name')->select()->getArray();
        return $this->success('ok', $list);
    }

    /**
     * 提交反馈🐉
     *
     * @param string $name 姓名
     * @param string $tel 电话
     * @param string $content 内容
     * @param string $images 图集
     * @param string $video_file 视频文件
     * @param string $feedbackcate_id 分类id
     * @return void
     * @Action
    */
    public function add($name = null, $tel = null, $content = null, $feedbackcate_id = null, $images = null, $video_file = null)
    {
        $data['name'] = $name;
        $data['tel'] = $tel;
        $this->VerifyStr($content);
        $this->VerifyStr($tel);
        $this->VerifyStr($name);
        $data['content'] = $content;
        $data['uid'] = $this->auth->user()->id;
        $data['feedbackcate_id'] = $feedbackcate_id;
        $data['images'] = $images;
        $data['video_file'] = $video_file;
        $res = Db::name('feedback')->whereEx($data)->find();
        if ($res) {
            return $this->error('已有相同的内容');
        } else {
            $data['time'] = time();
            Db::name('feedback')->insert($data);
            return $this->success('提交成功');
        }
    }


    /**
     * 删除意见🐉
     *
     * @param int $id 意见id
     * @return void
     * @Action
    */
    public function del($id = null)
    {
        $data['is_del'] = '1';
        $data['id'] = $id;
        $data['uid'] = $this->auth->user()->id;

        $res = Db::name('feedback')->whereEx($data)->find();
        if (!$res) {
            return $this->error('没有该信息');
        }
        Db::name('feedback')->whereEx($data)->update(['is_del' => '2', 'del_time' => time()]);

        return $this->success('删除成功');
    }



    /**
     * 获取我提交意见反列表🐉
     *
     * @param int $feedbackcate_id 分类id
     * @param int $status 状态:1=待处理,2=处理中,3=已处理
     * @param integer $page 1
     * @param integer $limit 8
     * @return void
     * @Action
    */
    public function get_my_feedback_list($feedbackcate_id = null,?int $page = 1,?int $limit = 8, $status = null)
    {
        $page = $page ?: 1;
        $limit = $limit ?: 8;
        $data['is_del'] = '1';
        $data['uid'] = $this->auth->user()->id;

        if ($feedbackcate_id)
            $data['feedbackcate_id'] = $feedbackcate_id;
        if ($status)
            $data['status'] = $status;

        $list = Db::name('feedback')->whereEx($data)->orderRaw('time desc')->page($page, $limit)->select()->getArray();
        if ($list)
            foreach ($list as $key => &$val) {
                $this->ChangeFieldData($val);
            }
        return $this->success('ok', $list);
    }

    /**
     * 获取意见反详情🐉
     *
     * @param int $id 公告id
     * @return void
     * @Action
    */
    public function get_feedback_data($id = null)
    {
        $data['is_del'] = '1';
        $data['id'] = $id;
        $data['uid'] = $this->auth->user()->id;

        $res = Db::name('feedback')->whereEx($data)->find();
        if (!$res) {
            return $this->error('没有该信息');
        }
        $this->ChangeFieldData($res);
        // $this->AddViews($id);

        return $this->success('ok', $res);
    }

    /**
     * 改变字段
     *
     * @param array $val 
     * @return void
     */
    protected function ChangeFieldData(&$val)
    {
        $val['date_str'] = $this->TimeToStr($val['time']);
        $val['cate'] = Db::name('feedbackcate')->where('id', '=', $val['feedbackcate_id'])->value('name');
        $val['date'] = date('Y-m-d H:i:s', $val['time']);
        return $val;
    }
}
